package com.hoolai.qps.mina.string;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class LoopClient extends Client {

    public static void main(String[] args) throws InterruptedException {
        LoopClient client = new LoopClient();
        ExecutorService executor = Executors.newCachedThreadPool();
        final NioSocketConnector connector = client.getConnector();
        for (int i = 0; i < 3000; i++) {
            System.err.println(i);
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    ConnectFuture future = connector.connect(new InetSocketAddress("192.168.20.17", 2222));
                    IoSession session = future.awaitUninterruptibly().getSession();
                    for (;;) {
                        IoBuffer buffer = IoBuffer.allocate(1, false);
                        session.write(buffer);
                        try {
                            TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            TimeUnit.MILLISECONDS.sleep(10);
        }
    }
}
